package org.example.demo;

import org.example.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class demo5_Update {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement statement  = null;


        try {
            //1.获取数据库连接
            connection = DBUtil.getConnection();
            //2.定义sql语句,根据id去修改学号
            String sql = "update users set name = ? where id = ?";
            //3.定义sql的预处理对象
            statement = connection.prepareStatement(sql);
            //4.用真实的数据替换占位符
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入要修改的id");
            long inId = scanner.nextLong();
            System.out.println("请输入修改之后的名字");
            String inName = scanner.next();


            statement.setString(1,inName);
            statement.setLong(2,inId);
            //5.执行sql
            int row = statement.executeUpdate();
            //6.获取执行结果
            if (row == 1){
                System.out.println("修改成功");
            }else {
                System.out.println("修改失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //释放资源
            DBUtil.close(null,statement,connection);
        }

    }
}



















